package at.jaki.ubb.crawling.demo.ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import at.jaki.ubb.crawling.api.CrawlingEngine;
import at.jaki.ubb.crawling.demo.model.UrlWithCount;
import at.jaki.ubb.crawling.demo.ui.browser.OpenBrowser;
import at.jaki.ubb.crawling.model.EngineStatus;

/**
 * singleton that manages the UI of the news miner.
 * 
 * @author Attila Jakabfi
 * 
 */
public class UiManager {

	private static final String TITLE = "International News miner - Crawling Engine Sample implementation";
	public static final String ACTION_GO = "go";
	public static final String ACTION_STATUS = "status";
	public static final String ACTION_RESET = "reset";
	private static UiManager instance;

	private UiManager() {
	}

	public static synchronized UiManager getInstance() {
		if (instance == null) {
			instance = new UiManager();
		}
		return instance;
	}

	private JFrame frame;
	private JPanel mainPanel, north, center;
	private JTextField search;

	private CrawlingEngine ce;
	private Listener listener;
	private OpenBrowser openBrowser = new OpenBrowser();

	public void init(CrawlingEngine ce, Listener listener) {
		this.ce = ce;
		this.listener = listener;
		initUi();
	}

	private void initUi() {
		frame = new JFrame(TITLE);
		frame.addWindowListener(listener);
		mainPanel = new JPanel(new BorderLayout());
		north = new JPanel();
		center = new JPanel(new BorderLayout());
		mainPanel.add(north, BorderLayout.NORTH);
		mainPanel.add(center, BorderLayout.CENTER);
		frame.getContentPane().add(mainPanel);

		initNorth();

		frame.setSize(800, 800);
		frame.setVisible(true);
	}

	private void initNorth() {
		JPanel p = new JPanel();
		search = new JTextField(40);
		JLabel label = new JLabel("Article URL");
		JButton button = new JButton();
		button.setText("GO");
		button.addActionListener(listener);
		button.setActionCommand(ACTION_GO);
		JButton button2 = new JButton();
		button2.setText("Status");
		button2.addActionListener(listener);
		button2.setActionCommand(ACTION_STATUS);
		JButton button3 = new JButton();
		button3.setText("Reset");
		button3.addActionListener(listener);
		button3.setActionCommand(ACTION_RESET);
		p.add(label);
		p.add(search);
		p.add(button);
		p.add(button2);
		p.add(button3);
		north.add(p);
	}

	public String getSearch() {
		return search.getText();
	}

	public void show(List<UrlWithCount> list) {
		center.setVisible(false);
		center.removeAll();
		JPanel panel = new JPanel(new GridLayout(list.size(), 1));
		for (UrlWithCount url : list) {
			JPanel p = new JPanel();
			JButton button = new JButton();
			button.addActionListener(openBrowser);
			button.setText("(" + url.getCount() + ") " + url.getUrl());
			button.setActionCommand(url.getUrl());
			p.add(button);
			panel.add(p);
		}
		JScrollPane pane = new JScrollPane(panel);
		center.add(pane);
		center.setVisible(true);
	}

	public void showStatus(EngineStatus status) {
		StringBuilder sb = new StringBuilder();

		sb.append("target qeuee size:" + status.getTargetQueuSize());
		sb.append(System.getProperty("line.separator"));

		sb.append("data set size:" + status.getDataSetSize());
		sb.append(System.getProperty("line.separator"));

		sb.append("running data finders:" + status.getRunningDataFinders());
		sb.append(System.getProperty("line.separator"));

		sb.append("running data transformers:" + status.getRunningDataTransofrmers());
		sb.append(System.getProperty("line.separator"));

		JOptionPane.showMessageDialog(frame, sb.toString());
	}

	public void clear() {
		center.setVisible(false);
		search.setText("");
		center.removeAll();
		center.setVisible(true);
	}
}
